<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #app {
            width: 100px;
            height: 100px;
            background-color: lightcoral;
        }
    </style>
</head>

<body>
    <div id="app" draggable="true">
        可以拖
    </div>
    <script>
        /* 单位时间以外,只触发一次,如果在这个单位时间之内多次触发,最终也只会执行一次 */
        /* 节流：无论你拖曳一个元素多快, */
        const app = document.getElementById("app");
        app.addEventListener("drag", throttle(function (e) {
            console.log(e.offsetX);
        }))
        function throttle(fn, delay = 500) {
            let timer = null;
            return function () {
                if (timer) {
                    return
                }
                timer = setTimeout(() => {
                    // arguments是函数内部的一个对象,接收函数传递过来的参数
                    console.log(arguments);
                    fn.apply(this, arguments);
                    timer = null
                }, delay)
            }
        }
    </script>
</body>

</html>